let mysql      = require('mysql');
let bdConfig      = require('../config/database').offline;


let product =  {
	list:function (config,callback) {
		let connection = mysql.createConnection(bdConfig);
		connection.connect();
		let f_in='',f_type='';
		if(config.ids){
			f_in = 'id in ('+(config.ids)+') and';
		}
		if(config.type){
			f_type = 'type in ('+config.type+') ';
		}
		let sql = 'SELECT * from t_product WHERE '+f_in +'  '+f_type+' Order By ' + config.orderBy + ' ' + config.sort + ' limit ' + config.start+','+config.limit;
		connection.query(sql, function(err, rows, fields) {
			callback(rows);
		});
		connection.end();
	},
	create:function () {
		return ({b:1});

		let  addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
		let  addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN'];
//增
		connection.query(addSql,addSqlParams,function (err, result) {
			if(err){
				console.log('[INSERT ERROR] - ',err.message);
				return;
			}

			console.log('--------------------------INSERT----------------------------');
			//console.log('INSERT ID:',result.insertId);
			console.log('INSERT ID:',result);
			console.log('-----------------------------------------------------------------\n\n');
			return result;
		});
		connection.end();
	},
	edit:function () {
		let modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?';
		let modSqlParams = ['菜鸟移动站', 'https://m.runoob.com',6];
//改
		connection.query(modSql,modSqlParams,function (err, result) {
			if(err){
				console.log('[UPDATE ERROR] - ',err.message);
				return;
			}
			console.log('--------------------------UPDATE----------------------------');
			console.log('UPDATE affectedRows',result.affectedRows);
			console.log('-----------------------------------------------------------------\n\n');
		});
		connection.end();
	},
	detail:function (id,callback) {
		let connection = mysql.createConnection(bdConfig);
		let sql = 'SELECT * from t_product where id= '+ id;
		connection.query(sql, function(err, rows, fields) {
			callback(rows);
		});
		connection.end();
	}
}

module.exports = product;


